﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Configuration;
using IHISCETeim.BLL;

namespace IHISCETeim.SendEMailSvr
{
    public class SyncSAPDAL
    {
        /// <summary>
        /// SBO连接字符串
        /// </summary>
        protected static string SBOConnStr
        {
            get
            {
                return "SBOConnectionString";
            }
        }
        private static Dictionary<string, string> _ConnStrInfo;
        /// <summary>
        /// 获取SQL联系字符串中的各个项,
        /// 项的标志全部为小写,并去掉项的空格符
        /// 如Data Source已变成datasource
        /// </summary>
        private static Dictionary<string, string> ConnStrInfo
        {
            get
            {
                ///Database DB = DatabaseFactory.CreateDatabase(SBOConnStr);
                if (_ConnStrInfo == null)
                {
                    string SBOconnstr = ConfigurationManager.ConnectionStrings[SBOConnStr].ConnectionString;
                    string[] connectionstring = SBOconnstr.Split(new char[] { ';' });
                    _ConnStrInfo = new Dictionary<string, string>();
                    foreach (string item in connectionstring)
                    {
                        string[] itemarr = item.Split(new char[] { '=' });
                        _ConnStrInfo.Add(itemarr[0].ToLower().Replace(" ", ""), itemarr[1]);
                    }
                }
                return _ConnStrInfo;
            }
        }

        protected static string DataSource
        {
            get
            {
                return ConnStrInfo["datasource"];
            }
        }
        protected static string InitialCatalog
        {
            get
            {
                return ConnStrInfo["initialcatalog"];
            }
        }
        protected static string UserID
        {
            get
            {
                return ConnStrInfo["userid"];
            }
        }
        protected static string Password
        {
            get
            {
                return ConnStrInfo["password"];
            }
        }

        /// <summary>
        /// sap 用于处理判断物料是否在基准BOM上有用到,PToolingCharge
        /// 返回 在基准BOM上已没用,并未发提示邮件的物料
        /// </summary>
        /// <returns></returns>
        public static DataTable GetPToolingChargeNoUseBom()
        {
            Database DB = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = DB.GetStoredProcCommand("SAP_GetPToolingChargeNoUseBom", DataSource, UserID, Password, InitialCatalog);
            try
            {
                return DB.ExecuteDataSet(dbCommand).Tables[1];
               
            }
            catch (Exception e)
            {
                return null;
            }
        }


        /// <summary>
        /// sap 用于处理判断物料是否在基准BOM上有用到, PToolingChargeDetail
        /// 返回 在基准BOM上已没用,并未发提示邮件的物料
        /// </summary>
        /// <returns></returns>
        public static DataTable GetPToolingChargeDetailNoUseBom()
        {
            Database DB = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = DB.GetStoredProcCommand("SAP_GetPToolingChargeDetailNoUseBom", DataSource, UserID, Password, InitialCatalog);
            try
            {
                return DB.ExecuteDataSet(dbCommand).Tables[1];

            }
            catch (Exception e)
            {
                return null;
            }
        }
        /// <summary>
        /// 更新 PToolingCharge 列 是否已发送BOM没用字段 值为 1
        /// </summary>
        /// <param name="ID"></param>
        public static void UpdatePToolingChargeIsSendNoBom(int ID)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = DB.GetSqlStringCommand(string.Format("UPDATE dbo.PToolingCharge SET IsSendNoBom=1 WHERE ID={0}",ID));
            try
            {
                DB.ExecuteNonQuery(dbCommand);
            }
            catch (Exception e)
            {
               
            }
        }

        /// <summary>
        /// 更新 PToolingCharge 列 是否已发送BOM没用字段 值为 1
        /// </summary>
        /// <param name="ID"></param>
        public static void UpdatePToolingChargeDetailIsSendNoBom(int ID)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = DB.GetSqlStringCommand(string.Format("UPDATE dbo.PToolingChargeDetail SET IsSendNoBom=1 WHERE ID={0}", ID));
            try
            {
                DB.ExecuteNonQuery(dbCommand);
            }
            catch (Exception e)
            {

            }
        }

      

        
        
    }
}
